<?php
declare (strict_types=1);
/**
 * Created by .
 * User: luogan
 * Date: 2021/12/30
 */

namespace App\Service;


use App\Exception\ErrorException;
use App\Model\Department;

class DepartmentService
{
    public function list($data)
    {
        $result = Department::getDeptTreeList($data);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    public function create($data)
    {
        $result = Department::create($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('新增部门');
        return $result;
    }

    public function update($data)
    {
        $result = Department::query()->where('id',$data['id'])->update($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('更新部门');
        return $result;
    }

    public function detail($id)
    {
        $result = Department::query()->find($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    public function delete($id)
    {
        $result = Department::destroy($id);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('删除部门');
        return $result;
    }
}
